package com.vueapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vueapi.domain.HjArticle;
import com.vueapi.domain.HjCategory;
import com.vueapi.domain.vo.CategoryCountVO;
import com.vueapi.mapper.HjCategoryMapper;
import com.vueapi.service.HjArticleService;
import com.vueapi.service.HjCategoryService;
import com.vueapi.utils.ParseUtils;
import com.vueapi.utils.ResultUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 15442
 * @description 针对表【hj_category】的数据库操作Service实现
 * @createDate 2022-09-20 11:44:51
 */
@Service
@Slf4j
public class HjCategoryServiceImpl extends ServiceImpl<HjCategoryMapper, HjCategory>
        implements HjCategoryService {

    @Autowired
    private HjArticleService articleService;

    @Override
    public ResultUtils listPage(Integer pageNum, Integer pageSize, String categorySearch) {
        Page<HjCategory> categoryPage = page(new Page<>(pageNum, pageSize), new LambdaQueryWrapper<HjCategory>()
                .eq(HjCategory::getIsDeleted, 0)
                .like(HjCategory::getName, categorySearch)
                .orderByDesc(HjCategory::getSort)
                .orderByDesc(HjCategory::getCreateTime));
        return ResultUtils.success(categoryPage);
    }

    @Override
    public ResultUtils top(HjCategory category) {
        HjCategory one = getOne(new LambdaQueryWrapper<HjCategory>().orderByDesc(HjCategory::getSort).last("limit 1"));
        if (category.getSort() < one.getSort()) {
            category.setSort(one.getSort() + 1);
        }
        updateById(category);
        return ResultUtils.success("置顶成功", null);
    }

    @Override
    public void updateArticleCount(Integer id) {
        this.baseMapper.updateArticleCount(id);
    }

    @Override
    public List<CategoryCountVO> getCategoryCount() {
        return list(new LambdaQueryWrapper<HjCategory>()
                .select(HjCategory::getName, HjCategory::getArticleCount))
                .stream()
                .map(c -> new CategoryCountVO(c.getName(), c.getArticleCount()))
                .collect(Collectors.toList());
    }

    @Override
    public Integer getCount() {
        return this.baseMapper.getCount();
    }

    @Override
    public ResultUtils getCategories() {
        LambdaQueryWrapper<HjCategory> wrapper = new LambdaQueryWrapper<HjCategory>().select(HjCategory::getId, HjCategory::getName,HjCategory::getArticleCount);
        List<HjCategory> categoryList = list(wrapper);
        return ResultUtils.success(categoryList);
    }

    @Override
    public List<String> checkAndRemoveByIds(String ids) {

        log.info("=========================Remove Category Start=========================");
        // 如果有文章关联此分类就不能删除
        // 查询已经被使用的分类
        List<String> useCategoryIds = articleService.getAllUseCategoryByIds(ParseUtils.intArrayToList(ParseUtils.stringToIntArray(ids)));
        if(useCategoryIds.isEmpty()){
            // 集合为空，直接循环删除
            this.removeByIds(Arrays.asList(ids.split(",")));
            log.info("没有正在使用的 category...");
        }
        log.info("当前正在使用的 category：" + useCategoryIds);
        log.info("=========================Remove Category End=========================");
        return useCategoryIds;
    }


}




